import { apiUrl } from "./index";
import type { User } from "@entity/User";
import { get, post, put } from "@util/http";

export const listUsers = async (): Promise<User[]> => {
  return await get<User[]>(apiUrl('/user'));
};

export const createUser = async (): Promise<User> => {
  return await post<User>(apiUrl(`/user`), null, null, {auth: false});
};

export const authUser = async (mock_id: string): Promise<User> => {
  return await post<User>(apiUrl(`/user/auth`), { mock_id }, null, {auth: false});
};

export const sendCode = async (mobile: string): Promise<string> => {
  return await post(apiUrl(`/user/sendcode`), { mobile });
}

export const signInUser = async (anonymousUser: User, mobile: string, code: string): Promise<User> => {
  return await post<User>(apiUrl(`/user/signin`), {...anonymousUser, mobile, code });
}

export const updateNickname = async (user: User, nickname: string): Promise<User> => {
  return await put<User>(apiUrl(`/user/nickname`), {id: user.id, nickname });
}

export const updateAvatar = async (user: User, avatar: string): Promise<User> => {
  return await put<User>(apiUrl(`/user/avatar`), {id: user.id, avatar });
}